<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- STYLESHEETS --><!--[if lt IE 9]>
    <script src="../../js/flot/excanvas.min.js"></script>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
    <link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
    <link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
    <link rel="stylesheet" type="text/css" href="../../css/responsive.css">
    <link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- SELECT2 -->
    <link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css"/>
    <!-- UNIFORM -->
    <link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css"/>
    <!-- datatable -->
    <link rel="stylesheet" href="../../js/datatables/datatable.css">
    <link rel="stylesheet" href="../../js/datepicker/datepicker.css">

    <!-- file-input -->
    <link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css"/>
    <!-- FONTS
    <link href='http://fonts.useso.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
-->
</head>
<body>
<header class="navbar clearfix" id="header">
</header>

<!-- PAGE -->
<section id="page">
    <!-- SIDEBAR -->
    <div id="sidebar" class="sidebar">
        <div class="sidebar-menu nav-collapse">

            <!-- SIDEBAR MENU -->
            <ul>
            </ul>
        </div>


    </div>
    <!-- /SIDEBAR -->
    <div id="main-content">
        <!-- SAMPLE BOX CONFIGURATION MODAL FORM-->

        <div class="container">
            <div class="row">
                <div id="content" class="col-lg-12">
                    <!-- PAGE HEADER-->
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="page-header">
                                <!-- STYLER -->

                                <!-- /STYLER -->
                                <!-- BREADCRUMBS -->
                                <ul class="breadcrumb">
                                    <li>
                                        <i class="fa fa-home"></i>
                                        <a href="index.html">首页</a>
                                    </li>
                                    <li>
                                        <a href="#">营销管理</a>
                                    </li>
                                    <li>优惠劵</li>
                                </ul>
                                <!-- /BREADCRUMBS -->

                            </div>
                        </div>
                    </div>
                    <!-- /PAGE HEADER -->
                    <!-- FORMS QUERY-->
                    <div class="row">
                        <div class="col-md-12">
                            <div class="row">
                                <div class="col-md-12"
                                     style="border: 1px solid #b6b6b6; margin-left: 15px;margin-bottom: 15px;width: 98%">

                                    <h5>1、订单模板请点击<a href="#" class="downloadOrderBtn">【这里】</a>下载</h5>
                                    <h5>2、保存订单前，请确保导入数据准确。点击保存后，系统会根据订单号自动拆分订单</h5>
                                </div>
                            </div>


                        </div>
                    </div>
                    <!-- /FORMS QUERY -->
                    <div class="row" style="height: 30px;">
                        <div class="col-md-12">
                            <div class="box ">
                                <div class="form-group">
                                    <label class="control-label col-md-1" style="line-height:34px;"><span class="required">*</span>销售平台：</label>
                                    <div class="col-md-2">
                                        <select id="platform_select" class="col-md-12 select2-offscreen" tabindex="-1">
                                            <option value='-1'>请选择</option>
                                        </select>
                                    </div>
                                </div>
                                <div style="float:left; margin-left:5px; margin-right: 10px" class="addnewimg" id="f_span">
                                    <span class="btn btn-success btn-file" id="fir">
                                        <span id="two">导入订单</span>
                                            <input name="uploadFile" upload-file="imageUrlProduct" type="file">
                                    </span>
                                </div>
                                <a href="javaScript:;" class="btn btn-success saveBtn">
                                    <i class="fa  fa-plus-square-o"></i> 保存订单
                                </a>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <!-- TABLES -->
                        <div class="col-md-12">
                            <!-- BOX -->
                            <div class="box border primary">
                                <div class="box-title">
                                    订单列表
                                </div>
                                <div class="box-body" style="min-height: 400px">
                                    <table id="db_Order_list" cellpadding="0" cellspacing="0" border="0"
                                           class="datatable table table-striped table-bordered table-hover">

                                    </table>
                                </div>
                            </div>
                            <!-- /BOX -->
                        </div>
                    </div>
                    <!-- /TABLES -->


                    <div class="footer-tools">
							<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
                    </div>
                </div><!-- /CONTENT-->
            </div>
        </div>
    </div>
</section>
<!-- JQUERY -->
<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
<script src="../../js/common.js"></script>
<!-- JQUERY UI-->
<script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
<!-- BOOTSTRAP -->
<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>
<!-- SLIMSCROLL -->
<script type="text/javascript" src="../../js/jQuery-slimScroll-1.3.0/jquery.slimscroll.min.js"></script>
<script type="text/javascript" src="../../js/jQuery-slimScroll-1.3.0/slimScrollHorizontal.min.js"></script>
<!-- BLOCK UI -->
<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
<!--bootbox-->
<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
<!-- AUTOSIZE -->
<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
<!-- spinner -->
<script src="../../js/spinner/spin.js"></script>
<!-- bootstrap-fileupload -->
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload.min.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-process.min.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-validate.min.js"></script>

<!-- INPUT MASK -->
<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>

<!-- SELECT2 -->
<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
<!-- UNIFORM -->
<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
<!-- WIZARD -->
<script src="../../js/bootstrap-wizard/jquery.bootstrap.wizard.min.js"></script>
<!-- WIZARD -->
<script src="../../js/jquery-validate/jquery.validate.min.js"></script>
<script src="../../js/jquery-validate/additional-methods.min.js"></script>
<script src="../../js/bootstrap-wizard/form-wizard.js"></script>
<!-- NESTABLE LISTS -->
<script type="text/javascript" src="../../js/nestable/jquery.nestable.min.js"></script>
<!-- COOKIE -->
<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
<!-- CUSTOM SCRIPT -->
<script src="../../js/script.js"></script>

<script src="../../js/Sortable.min.js"></script>

<!-- DATA TABLES -->
<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
<script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>


<script>

    $(function () {
        var selectedTable;
        $("#header").load("../head.html");
        App.init(); //Initialise plugins and elements
        initPlatform();

        function initPlatform() {
            console.log(11111)
            $.ajax({
                type: "get",
                url: App.getContextPath() + "admin/wholesaler/findPlatformList.do",
                async: true,
                success: function (result) {
                    var list = result.extra;
                    var platform = "";
                    for (var i = 0; i < list.length; i++) {
                        platform += " <option value='" + list[i].platformNo + "'>" + list[i].platformName + "</option>";
                    }
                    $("#platform_select").append(platform);
                }
            });
            $("#platform_select").select2({placeholder: "请选择销售平台"});
        }

        $(".downloadOrderBtn").click(function () {
            location.href = App.getContextPath() + "admin/file/downloadTemplate.do?template=ImportOrder";
        });

        $("[upload-file=imageUrlProduct]").fileupload({
            url: App.getContextPath() + "admin/order/uploadOrderExcel.do",
            dataType: 'json',
            acceptFileTypes: /(\.|\/)(xlsx)$/i,
            iframe: true,
            start: function (e) {
                $('.progress').fadeIn(300);
                return true;
            },
            autoUpload: true,
            done: function (e, data) {
                console.log(data.result);
                if (data.result.type == 'error') {
                    $.alert("导入模板错误");
                } else {
                    initDateTable(data.result.extra);
                }

            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('.progress .progress-bar').css('width', progress + '%');
            }
        }).on('fileuploaddone', function (e, data) {
            $(".progress").fadeOut(300);

        });

        var initDateTable = function (data) {
            var dtSetting = $.dataTableSetting({
                "serverSide": false,
                "bFilter": false,// 去掉搜索框
                "sScrollX": "100%",
                "sScrollXInner": "100%",
                "bSort": false,
                "paging": false,
                "data": data,
                "aoColumns": [{
                    "mDataProp": "outCode",
                    "sTitle": "外部订单号",
                    "width": "6%"
                }, {
                    "mDataProp": "skuCode",
                    "sTitle": "SKU编码",
                    "width": "6%"
                }, {
                    "sTitle": "商品名称",
                    "width": "10%",
                    "render": function (data, type, row) {
                        return row.skuBasicData ? row.skuBasicData.productName : "";
                    }
                }, {
                    "sTitle": "尺码大小",
                    "width": "6%",
                    "render": function (data, type, row) {
                        return row.skuBasicData ? row.skuBasicData.sizeName : "";
                    }
                }, {
                    "mDataProp": "quantity",
                    "sTitle": "数量",
                    "width": "6%"
                }, {
                    "mDataProp": "salePrice",
                    "sTitle": "单价",
                    "width": "6%"
                }, {
                    "mDataProp": "receiver",
                    "sTitle": "收货人姓名",
                    "width": "6%"
                }, {
                    "mDataProp": "telPhone",
                    "sTitle": "收货人手机号",
                    "width": "6%"
                }, {
                    "mDataProp": "address",
                    "sTitle": "收货地址",
                    "width": "6%"
                }, {
                    "mDataProp": "errorMsg",
                    "sTitle": "备注",
                    "width": "10%",
                    "render": function (data, type, row) {
                        return "<span style='color:red'>" + row.errorMsg ? row.errorMsg : "" + "</span>";
                    }
                }]

            });

            selectedTable = $("#db_Order_list").dataTable(dtSetting);

        }

        function contains(arr, obj) {
            var i = arr.length;
            while (i--) {
                if (arr[i].outCode === obj.outCode) {
                    return true;
                }
            }
            return false;
        }

        $(".saveBtn").click(function () {
            if (selectedTable == null)
                return;
            var paltformNo = $("#platform_select").val();
            var platformName = $("#platform_select").find("option:selected").text();
            console.log(paltformNo + "  " + platformName)
            if (paltformNo == -1) {
                $.alert("请选择销售平台");
                return;
            }

            var obj = selectedTable.api().rows().data();
            var datas = [];

            var amount = 0;
            for (var i = 0; i < obj.length; i++) {
                var errorMsg = obj[i].errorMsg;
                console.log(errorMsg)
                if (errorMsg) {
                    $.alert("订单数据有误");
                    return;
                }
                amount += (obj[i].salePrice) * (obj[i].quantity);
                datas.push(obj[i]);
            }
            var noRepeatArr = [];
            datas.forEach(function (e) {
                if (!contains(noRepeatArr, e)) {
                    noRepeatArr.push(e);
                }
            });
            if (datas === 0)
                return;

            var param={};
            param.platformNo = paltformNo;
            param.platformName = platformName;
            param.detailList = datas;
            var title = "订单总数:" + noRepeatArr.length + "笔<br>商品金额(不包括运费):" + amount + "元<br>确认要生成订单吗？"
            $.confirm(title, function () {

                param = JSON.stringify(param);
                $.ajax({
                    type: "post",
                    url: App.getContextPath() + "admin/order/saveOutOrderByImport.do",
                    data: param,
                    contentType: 'application/json',
                    async: true,
                    success: function (result) {
                        if (result.type === "error") {
                            $.alert(result.content);
                        } else {
                            location.href = "order_list.html";
                        }
                    }
                });
            })

        });

    });
</script>
</body>
</html>